<template>
	<view>
		<u-popup :show="popup_show" mode="center" @close="onClose()" :safeAreaInsetBottom="false" :mask-close-able="false" bgColor="#ffffff" round="15">
			<view class="popup">
				<view class="popup-wrap">
					<view class="title-line">
						<text class="title">{{title}}</text>
						<!-- <image src="@/static/images/close.png" class="close-ioc" @tap="cancel()"></image> -->
					</view>
					<scroll-view scroll-y="true" class="scroll-y" @scrolltolower="scrolltolower()">
						<view class="content">
							<rich-text :nodes="detailsData.content"></rich-text>
						</view>
					</scroll-view>

					<view class="btn-view">
						<button class="bg-orange cu-btn" @tap="onAgree()" v-if="checked">已同意</button>
						<button class="bg-orange cu-btn" :disabled="true" v-else-if="time >=4">请滚动阅读</button>
						<button class="bg-orange cu-btn" :disabled="true" v-else-if="time > 0">{{time}}S</button>
						<button class="bg-orange cu-btn" @tap="onAgree()" v-else>下一步</button>
					</view>

				</view>
			</view>
		</u-popup>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import htmlParser from '@/common/html-parser';
	export default {

		data() {
			return {
				id: null,
				title: "",
				detailsData: { content: "" },
				popup_show: false,
				time: 4,
				timer: null,
				checked: false,
			};
		},

		methods: {
			show(id, title, checked) {

				uni.showLoading({
					title: "加载中"
				})
				this.id = id
				this.title = title
				this.checked = checked
				this.detailsData = { content: "" }
				this.loadData({ id: id });
				if (checked) {
					this.time = 0;
				} else {
					this.time = 4;
				}
				clearInterval(this.timer)
				this.popup_show = true
			},
			// 异步加载
			async loadData(data) {
				await uni.request({
					url: '/wanlshop/article/details',
					data: data,
					success: res => {
						//详情正则
						res.data.content = res.data.content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, (match, capture) => {
							var img = `<img style="display: block; max-width: 100%;" src="${this.$wanlshop.oss(capture, 500, 0)}">`;
							return img;
						});
						this.detailsData = res.data;

					},
					complete: () => {
						uni.hideLoading()
					}
				});
			},

			confirm() {
				this.popup_show = false
				this.$emit('confirm')
			},
			onClose() {
				clearInterval(this.timer)
			},
			cancel() {
				this.popup_show = false
				this.$emit('cancel')
			},
			startTimeDown() {
				if (this.time === 0) return false
				this.time = 3
				clearInterval(this.timer)
				this.timer = setInterval(() => {
					this.time--
					if (this.time <= 0) {
						clearInterval(this.timer)
					}
				}, 1000)
			},
			onAgree() {

				this.popup_show = false
				setTimeout(() => {
					this.$emit("agree", this.id)
				}, 500)
			},
			scrolltolower() {
				console.log(11111);
				this.startTimeDown()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.popup {
		width: 710rpx;
	}

	.popup-wrap {
		background: #ffffff;
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		border-radius: 20rpx;

		.title-line {
			display: flex;
			align-items: center;
			justify-content: center;
			position: relative;

			.title {
				margin: 0 auto;
				line-height: 130rpx;
				font-size: 38rpx;
				color: #000000;
				font-weight: bold;
			}

			.close-ioc {
				position: absolute;
				width: 30rpx;
				height: 30rpx;
				right: 30rpx;
			}
		}

		.scroll-y {
			height: 70vh;
		}

		.content {
			padding: 0rpx 30rpx;
		}

		.btn-view {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin: 30rpx 40rpx 30rpx;

			.bg-orange {
				width: 100%;
			}

			.bg-orange {}


		}
	}
</style>